package com.overseasShopping.Action;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Hibernate;

import com.opensymphony.xwork2.ActionSupport;
import com.overseasShopping.Biz.DetailUserBiz;
import com.overseasShopping.Biz.UserBiz;
import com.overseasShopping.pojo.DetailUser;
import com.overseasShopping.pojo.User;
import com.overseasShopping.pojo.Wallet;

public class AddUserAction extends ActionSupport{
	private String uid;
	private String nickname;
	private String realName;
	private String sex;
	private String psw1;
	private String telephone;
	private String email;
	private UserBiz userBiz;
	private DetailUserBiz detailUserBiz;
	
	
	public void setUid(String uid) {
		this.uid = uid;
	}


	public void setStr(String str) {
	}


	public void setNickname(String nickname) {
		this.nickname = nickname;
	}


	public void setRealName(String realName) {
		this.realName = realName;
	}


	public void setSex(String sex) {
		this.sex = sex;
	}


	public void setPsw1(String psw1) {
		this.psw1 = psw1;
	}





	public void setPsw2(String psw2) {
	}


	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}


	public void setEmail(String email) {
		this.email = email;
	}


	public String getUid() {
		return uid;
	}


	public String getNickname() {
		return nickname;
	}


	public String getRealName() {
		return realName;
	}


	public String getSex() {
		return sex;
	}


	public String getPsw1() {
		return psw1;
	}


	public String getTelephone() {
		return telephone;
	}


	public String getEmail() {
		return email;
	}


	@Override
	public String execute() throws Exception {
		User user=new User(uid,psw1);
		String realpath=ServletActionContext.getServletContext().getRealPath("/");
		String path=realpath+"img/defualtimg.jpg";
		FileInputStream is=new FileInputStream(path);
		byte[] bytes=new byte[is.available()];
		is.read(bytes);
		is.close();
		DetailUser detailUser=new DetailUser(uid,nickname,realName,sex,telephone,Hibernate.createBlob(bytes), "无",email,"0");

		detailUser.setUimage(Hibernate.createBlob(bytes));
		Wallet wallet=new Wallet(uid,(float)0);
		
		user.setWallet(wallet);
		user.setDetailUser(detailUser);
		userBiz.addUser(user);
		return SUCCESS;
	}
	
	public String userVerification() throws IOException{
		System.out.println("用户名验证》。。。");
		System.out.println(uid);
		HttpServletResponse response = ServletActionContext.getResponse(); 
		response.setContentType("text/plain;charset=UTF-8"); 
		PrintWriter out=response.getWriter();
		User user=userBiz.getUser(uid);
			if(user==null){
				out.print("success");
			}else{
				out.print("fail");
			}
			out.flush();
			out.close();
			
		return SUCCESS;
	}

	
	public UserBiz getUserBiz() {
		return userBiz;
	}


	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}


	public DetailUserBiz getDetailUserBiz() {
		return detailUserBiz;
	}


	public void setDetailUserBiz(DetailUserBiz detailUserBiz) {
		this.detailUserBiz = detailUserBiz;
	}


	
}
